package com.pingan.generator.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import com.pingan.generator.api.service.ApiQueryService;

@Controller
@RequestMapping("/api")
@PropertySource("classpath:application.properties")
public class ApiQueryController {
	
    @Autowired
	private ApiQueryService apiQueryService;
    
    @Value("${server.port}")
    private String reqHost;
    @Value("${server.ip}")
    private String reqUrl;
     /**
      * 查询所有的API接口
      * @param request
      */
     @RequestMapping(value="/getApiList",method = RequestMethod.POST)
 	 @ResponseBody
     public String getApiList(HttpServletRequest request){
    	 String result=null;
    	 try{
    		 List<Map<String,Object>> retList=apiQueryService.getApiList();
    		 Map<String,Object> retMap=new HashMap<String,Object>();
    		 retMap.put("apiList", retList);
    		 result=getResultMsg(retMap,200,"请求成功");
    	 }catch(Exception e){
    		 e.printStackTrace();
    		 result=getResultMsg(null,400,"服务异常");
    	 }
    	 return result;
     }
     
     /**
      * 确认Api接口
      * @param request
      * @return
      */
     @RequestMapping(value="/sureApi",method = RequestMethod.POST)
 	 @ResponseBody
     public String sureApi(HttpServletRequest request){
    	 String id=request.getParameter("id");
    	 String result=null;
    	 if("".equals(id)||null==id){
    		 result=getResultMsg(null,300,"API接口Id不为空");
    	 }else{
    		 try{
    			 long key=Long.valueOf(id);
    			 apiQueryService.sureApi(key);
    			 Map<String,String> retMap=new HashMap<String,String>();
    			 retMap.put("flag", "0");
    			 System.out.println("确认api接口ok...");
    			 result=getResultMsg(retMap,200,"请求成功");
    		 }catch(Exception e){
    			 e.printStackTrace();
    			 result=getResultMsg(null,400,"服务异常");
    		 }
    	 }
    	 return result;
     }
     
     /**
      * 根据api接口获取到接口的数据(入参，出参等信息)
      * @param request
      */
     @RequestMapping(value="/getApiDetail",method = RequestMethod.POST)
     @ResponseBody
     public String getApiDetail(HttpServletRequest request){
    	 String result=null;
    	 String colFatherId=request.getParameter("colFatherId");
    	 String apiType=request.getParameter("apiType");
    	 System.out.println("入参::colFatherId=="+colFatherId+"   apiType::"+apiType);
    	 if("".equals(colFatherId)||null==colFatherId||"".equals(apiType)||null==apiType){
    		 result=getResultMsg(null,300,"入参为空");
    	 }else{
    		 Map<String,Object> retMap=null;
        	 try{
        		 long fatherId=Long.valueOf(colFatherId);
        		 retMap=apiQueryService.getApiDetail(fatherId,apiType);
        		 result=getResultMsg(retMap,200,"请求成功");
        	 }catch(Exception e){
        		 e.printStackTrace();
        		 result=getResultMsg(null,400,"服务异常");
        	 }
    	 }
    	 return result; 
     }
     
     /**
      * 转为json格式返回页面
      * @param obj
      * @param code
      * @param msg
      * @return
      */
     private String getResultMsg(Object obj,int code,String msg){
    	 JSONObject json=new JSONObject();
    	 json.put("code", code);
    	 json.put("msg", msg);
    	 json.put("data", obj);
    	 
    	 String response=json.toJSONString();
    	 System.out.println("返回前端结果::"+response);
    	 return response;
     }
     
     /**
      * 初始化API页面（jquery）
      * @return
      */
     @RequestMapping(value="/index",method = RequestMethod.GET)
     public ModelAndView getApiView(ModelMap map){
    	 String url=reqUrl+":"+reqHost;
    	 System.out.println("url::"+url);
    	 map.addAttribute("url", url);
    	 ModelAndView model=new ModelAndView("apiShow",map);
    	 return model;
     }
     
       
     
     
}
